package 
{
	import flash.display.*;
	import flash.events.*;
	import flash.net.*;
	import flash.utils.*;
	import flash.media.*;
	import flash.text.*;
	
	[SWF(width = "328", height="64", backgroundColor="0xffffff")]
	
	public class Main extends Sprite 
	{	
		// embedding
		
		[Embed(source = "play.png", mimeType = "image/png")]
		private var Pimg:Class;
		private var playBmp:Bitmap = new Pimg();
		
		[Embed(source = "stop.png", mimeType = "image/png")]
		private var Simg:Class;
		private var stopBmp:Bitmap = new Simg();
		
		[Embed(source = "next.png", mimeType = "image/png")]
		private var Nimg:Class;
		private var nextBmp:Bitmap = new Nimg();
		
		[Embed(source = "prev.png", mimeType = "image/png")]
		private var Primg:Class;
		private var prevBmp:Bitmap = new Primg();
		
		// embedding
		
		private var output:TextField = new TextField();
		private var oformat:TextFormat = new TextFormat("courier", 16);
		
		//switchers
		private static var psSw:Boolean = true;
		private static var chSw:Boolean = false;
		private static var dragPick:Boolean = false;
		
		private static var trpos:Number = 0;
		//end switchers
		
		private var timer:Timer = new Timer(500);
		
		private var timeScreen:TextField = new TextField();
		private var id3Screen:TextField = new TextField();
		private var tformat:TextFormat = new TextFormat("Verdana", 12, 0x000000);
		
		private var progressBar:Sprite = new Sprite();
		private var progressText:TextField = new TextField();
		private var progressProcess:TextField = new TextField();
		
		private var volumePick:TextField = new TextField();
		private var volumeBg:TextField = new TextField();
		private var volumeBar:Sprite = new Sprite();
		
		private var playButton:Sprite = new Sprite();
		private var nextButton:Sprite = new Sprite();
		private var prevButton:Sprite = new Sprite();
		private var prc:Number = 0.8;
		
		//sound
		private var playList:Array = new Array();
		private static var songPointer:int = 0;
		private var req:URLRequest = new URLRequest("http://mixailfirst.narod.ru/blog/mp3/rother/protector-rother.mp3");
		private var loader:URLLoader = new URLLoader();
		private var snd:Sound = new Sound();
		private var sch:SoundChannel = new SoundChannel();
		
		public function Main():void 
		{	
			stage.align = StageAlign.TOP_LEFT;
			
			//test
			output.defaultTextFormat = oformat;
			output.width = 500;
			output.height = 300;
			output.border = true;
			output.y = 100;
			addChild(output);
			
			
			//end test
			
			
			timer.addEventListener(TimerEvent.TIMER, timerHnd);
			
			timeScreen.x = 8;
			timeScreen.y = 8;
			timeScreen.width = 96;
			timeScreen.height = 16;
			timeScreen.defaultTextFormat = tformat;
			timeScreen.border = true;
			addChild(timeScreen);
			timeScreen.text = "11:00";
			
			id3Screen.x = 104;
			id3Screen.y = 8;
			id3Screen.width = 216;
			id3Screen.height = 16;
			id3Screen.defaultTextFormat = tformat;
			id3Screen.border = true;
			addChild(id3Screen);
			id3Screen.text = "sssssssss:00";
			
			playButton.addChild(stopBmp);
			playButton.addChild(playBmp);
			playButton.x = 8;
			playButton.y = 24;
			addChild(playButton);
			
			playButton.addEventListener(MouseEvent.CLICK, playHnd);
			
			prevButton.addChild(prevBmp);
			prevButton.x = 40;
			prevButton.y = 24;
			addChild(prevButton);
			
			nextButton.addChild(nextBmp);
			nextButton.x = 72;
			nextButton.y = 24;
			addChild(nextButton);
			
			
			progressText.defaultTextFormat = tformat;
			progressText.border = true;
			progressText.background = true;
			progressText.backgroundColor = 0xeeccaa;
			progressText.text = "load:";
			progressText.width = 216;
			progressText.height = 16;
			
			progressProcess.width = 0;
			progressProcess.height = 14;
			progressProcess.background = true;
			progressProcess.backgroundColor = 0xEEEEEE;
			progressProcess.y = 1;
			progressProcess.x = 1;
			progressBar.x = 104;
			progressBar.y = 24;
			
			var tf:TextField = new TextField();
			tf.width = 216;
			tf.height = 16;
			tf.border = true;
			progressBar.addChild(tf);
			progressBar.addChild(progressText);
			progressBar.addChild(progressProcess);
			
			addChild(progressBar);
			progressBar.addEventListener(MouseEvent.CLICK, rewHnd);
			
			volumeBg.width = 216;
			volumeBg.height = 16;
			volumeBg.border = true;
			volumeBg.selectable = false;
			volumePick.width = 32;
			volumePick.height = 16;
			
			volumePick.border = true;
			volumePick.selectable = false;
			volumePick.background = true;
			volumePick.backgroundColor = 0xaaaaaa;
			volumeBar.x = 104;
			volumeBar.y = 40;
			
			
			volumeBar.addChild(volumeBg);
			volumeBar.addChild(volumePick);
			volumePick.x = volumeBar.width - 32 ;
			addChild(volumeBar)
			
			volumePick.addEventListener(MouseEvent.MOUSE_DOWN, volHnd);
			volumePick.addEventListener(MouseEvent.MOUSE_UP, volHnd);
			volumeBar.addEventListener(MouseEvent.MOUSE_MOVE, volHnd);
			
			
			snd.addEventListener(Event.OPEN, sopenHnd);
			snd.addEventListener(ProgressEvent.PROGRESS, progressHnd);
			snd.addEventListener(Event.COMPLETE, cmpHnd);
			snd.addEventListener(IOErrorEvent.IO_ERROR, sndErr);
			
			var param:Object = LoaderInfo(this.root.loaderInfo).parameters;
			var keys:String;
			var vals:String;
			for (keys in param) {
				if (keys == "file") {
					req.url = param[keys];
					snd.load(req);	
					}
				if (keys == "list") {
					req.url = param[keys];
					loader.load(req);
					loader.addEventListener(Event.COMPLETE, loaderHnd);
					
					}
				}
			
			
			
			
			timer.start();
			
		}
		
		private function msg(str:String = ""):void {
			
			id3Screen.text = str;
			}
		
		private function omsg(str:String):void
		{
			output.appendText(str+"\n");
			
			}
		
		private function playHnd(event:MouseEvent):void
		{
			if (psSw) {
				if (chSw) {
					trpos = 0;
					chSw = false;
					}
				sch = snd.play(trpos,0,new SoundTransform(prc,0));
				
				progressProcess.visible = new Boolean(true);
				sch.addEventListener(Event.SOUND_COMPLETE, soundEnd);
				var artist:String = snd.id3.artist;
				var song:String = snd.id3.songName;
				msg("playing"+artist+" : "+song);
				psSw = false;
				event.target.getChildAt(1).visible=false;
			}else{
				trpos = sch.position;
				sch.stop();
				event.target.getChildAt(1).visible=true;
				psSw = true;
				}
		
		}
		
		private function volHnd(event:MouseEvent):void
		{
			var evtype:String = event.type.toString();
			if (evtype == "mouseDown") {
				
				dragPick = true;
				}
			
			if (evtype == "mouseUp") {
				
				dragPick = false;
				}	
				
			if (evtype == "mouseMove"&&dragPick) {
				
				if(event.stageX>104+16&&event.stageX<320-16){
					volumePick.x = event.stageX - (volumePick.width / 2) - 104;	
					prc = 1/(volumeBar.width/(volumePick.x+16));
					//msg(volumeBar.width.toString()+" "+volumePick.x.toString()+" prec"+prc.toString()+" "+String(Math.round(prc*100)));
					msg(volumeBar.width.toString()+" "+volumePick.x.toString()+" prec"+String(Math.round(prc*100)));
					sch.soundTransform = new SoundTransform(prc,0);
					}	
				}
			
			}
		
			
			private function soundEnd(event:Event):void {
				
				trpos = 0;
				playButton.getChildAt(1).visible = true;
				psSw = true;
				}	
				
			private function timerHnd(event:TimerEvent):void {
				
				var secPass:int = Math.floor(sch.position) / 1000;
				var minPass:int = Math.floor(secPass/60);
				timeScreen.text = minPass.toString()+":"+Math.round(secPass%60).toString();
				
				if(sch.position!==0){
				progressProcess.width = 216 / (100/(100 / ( snd.length/sch.position)));
				}
				if(sch.position==0){
				progressProcess.width=0;
				}
				}
				
			private function sopenHnd(event:Event):void {
				omsg("load start");
				
				}
				
			private function progressHnd(event:ProgressEvent):void {
						
					var bload:uint = event.target.bytesLoaded;
					var btotal:int = event.target.bytesTotal;
					var prcload:int = 100/(btotal/bload);
					progressText.text = "loading:"+100/(btotal/bload)+"%";
					progressText.width = 216 / (100 / prcload);
					progressProcess.visible = false;
				}
				
			private function cmpHnd(event:Event):void {
					id3Screen.text = snd.id3.artist + " - " + snd.id3.songName;
					
				}	
			
				
			private function rewHnd(event:MouseEvent):void
			{
				var mposx:int = event.stageX;
				var prcp:int = 100 / (216 / (mposx - 104)); 
				trpos = snd.length / (100 / prcp);
				sch.stop();
				sch = snd.play(trpos);
				timer.stop();
				timer.start();
				msg(String(snd.length/(100/prcp)));
					
				}
				
				
		private function loaderHnd(event:Event):void {
			omsg("load OK");
			var fstr:String = event.target.data.toString();
			//omsg(String(event.target.data));
			var re:RegExp = new RegExp("(http://.+)", "ig");
			playList = fstr.match(re);
			var i:int;
			for (i = 0; i < playList.length; i++ ) {
				omsg(playList[i].toString())
				
				}
				req.url = playList[0];
				snd.load(req);
				nextButton.addEventListener(MouseEvent.CLICK, nextHnd);
				prevButton.addEventListener(MouseEvent.CLICK, prevHnd);
			}
			
			private function nextHnd(event:MouseEvent):void
			{
				if(songPointer<playList.length){
					songPointer+=1;
					sch.stop();
					omsg(sch.toString());
					snd.removeEventListener(ProgressEvent.PROGRESS, progressHnd);
					snd.removeEventListener(Event.COMPLETE, cmpHnd);
					snd.removeEventListener(Event.OPEN, sopenHnd);
					playButton.getChildAt(1).visible = true;
					req = new URLRequest(String(playList[songPointer]));
					omsg(req.url.toString());
					snd = new Sound();
					snd.load(req);
					snd.addEventListener(ProgressEvent.PROGRESS, progressHnd);
					snd.addEventListener(Event.COMPLETE, cmpHnd);
					snd.addEventListener(Event.OPEN, sopenHnd);
					chSw = true;
					psSw = true;
					
					//progressProcess.width = new int(0);
					omsg("snd url:"+snd.url.toString());
					
					
				}
				
				}
			
			private function prevHnd(event:MouseEvent):void
			{
				if(songPointer>0){
					songPointer--;
					sch.stop();
					omsg(sch.toString());
					snd.removeEventListener(ProgressEvent.PROGRESS, progressHnd);
					snd.removeEventListener(Event.COMPLETE, cmpHnd);
					snd.removeEventListener(Event.OPEN, sopenHnd);
					playButton.getChildAt(1).visible = true;
					req = new URLRequest(String(playList[songPointer]));
					omsg(req.url.toString());
					snd = new Sound();
					snd.load(req);
					snd.addEventListener(ProgressEvent.PROGRESS, progressHnd);
					snd.addEventListener(Event.COMPLETE, cmpHnd);
					snd.addEventListener(Event.OPEN, sopenHnd);
					
					
					chSw = true;
					psSw = true;
					omsg("snd url:"+snd.url.toString());
					
					
				}
				
				}
				
				
				private function sndErr(event:IOErrorEvent):void {
					omsg("error!!!:"+event.errorID.toString());
					
					}
			
	}//class
	
}